package cn.org.intelli.zjgflink.aop;


import cn.org.intelli.zjgflink.exception.CustomException;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Configuration
public class AuthenticationTicketAop {
    private static final Logger log = LoggerFactory.getLogger(cn.org.intelli.zjgflink.aop.AuthenticationTicketAop.class);

    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    public void controllerGetMappingPointcut() {
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public void controllerPostMappingPointcut() {
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
    public void controllerDeleteMappingPointcut() {
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.PutMapping)")
    public void controllerPutMappingPointcut() {
    }

    @Before("controllerGetMappingPointcut()")
    public void getAuthenticationTicketFormGetMapping(JoinPoint joinPoint) throws Exception {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        setAuthenticationTicket(request);
    }

    @Before("controllerPostMappingPointcut()")
    public void getAuthenticationTicketFormPostMapping(JoinPoint joinPoint) throws Exception {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        setAuthenticationTicket(request);
    }

    @Before("controllerDeleteMappingPointcut()")
    public void getAuthenticationTicketFormDeleteMapping(JoinPoint joinPoint) throws Exception {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
    }

    private void setAuthenticationTicket(HttpServletRequest request) throws CustomException {
        String pin = (request.getHeader("X-Consumer-Username") != null) ? request.getHeader("X-Consumer-Username") : request.getHeader("x-credential-username");
        String token = request.getHeader("token");
        if (pin == null && token == null)
            throw new CustomException("");
        if (pin != null) {
            AuthenticationTicket.setTicket(new AuthenticationTicket(pin));
        } else if (token != null &&
                StringUtils.equals(token, "zhangyan")) {
            AuthenticationTicket.setTicket(new AuthenticationTicket(token));
        }
    }
}
